package com.swt.gap.controller;

import com.swt.gap.model.vo.Model2VO;
import com.swt.gap.model.vo.ModelByClassVO;
import com.swt.gap.service.Model2Service;
import com.swt.gap.util.UserLoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "模型v2")
@RestController
@RequestMapping("/model/v2")
@Slf4j
public class Model2Controller {

    private final Model2Service model2Service;

    public Model2Controller(Model2Service model2Service) {
        this.model2Service = model2Service;
    }

    @PostMapping
    @ApiOperation(value = "创建", notes = "创建模型")
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED", response = Model2VO.class)})
    public ResponseEntity<Model2VO> saveModel(@RequestBody Model2VO model) {
        int userId = UserLoginUtils.getUserId();
        model.setDeleted(false);
        model.setCreatedBy(userId);
        model.setCreatedTime(new Date());
        model2Service.saveModel(model);
        return ResponseEntity.ok(model);
    }

    @GetMapping("/{modelId}")
    @ApiOperation(value = "根据id获取模型", notes = "根据id获取模型")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = Model2VO.class)})
    public ResponseEntity<Model2VO> getModel(@PathVariable Integer modelId) {
        return ResponseEntity.ok(model2Service.getModelById(modelId));
    }

    @PutMapping("/{modelId}")
    @ApiOperation(value = "修改模型", notes = "修改模型")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = Model2VO.class)})
    public ResponseEntity<Model2VO> updateModel(@PathVariable Integer modelId, @RequestBody Model2VO model) {
        int userId = UserLoginUtils.getUserId();
        model.setModelId(modelId);
        model.setUpdatedBy(userId);
        model.setUpdatedTime(new Date());
        model2Service.updateModel(model);
        return ResponseEntity.ok(model);
    }

    @DeleteMapping("/{modelId}")
    @ApiOperation(value = "删除模型", notes = "删除模型")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    public ResponseEntity<Boolean> deleteModel(@PathVariable Integer modelId) {
        int userId = UserLoginUtils.getUserId();
        Model2VO model = new Model2VO();
        model.setModelId(modelId);
        model.setDeleted(true);
        model.setUpdatedBy(userId);
        model.setUpdatedTime(new Date());
        model2Service.updateModel(model);
        return ResponseEntity.ok(true);
    }


    @GetMapping
    @ApiOperation(value = "获取模型列表", notes = "获取模型列表")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = ModelByClassVO.class, responseContainer = "List")})
    public ResponseEntity listModel() {
        return ResponseEntity.ok(model2Service.listModel());
    }


    @PostMapping("/recommend-model")
    @ApiOperation(value = "推荐模型", notes = "推荐模型")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = Model2VO.class)})
    public ResponseEntity<Model2VO> getModel(@RequestBody List<String> header) {
        return ResponseEntity.ok(model2Service.getRecommendModel(header));
    }
}
